import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jspsmart.upload.SmartUpload;

import export.ExportHandle;


public class DownLoadServlet extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private ServletConfig config;

	private String filename;

	private String filepath;
	
	private String filetype;

	/**
	 * Init the servlet
	 */

	final public void init(ServletConfig config) throws ServletException {
		this.config = config;
	}

	final public ServletConfig getServletConfig() {
		return config;
	}

	/**
	 * Handles GET requests
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		filename = request.getParameter("filename");
		filepath = request.getParameter("filepath");
		filetype=request.getParameter("filetype");
		SmartUpload mySmartUpload = new SmartUpload();
		// Initialization
		try {
//			File fileWrite = new File(filepath+filename);
//			fileWrite.createNewFile();
//			OutputStream os = new FileOutputStream(fileWrite);
//			ExportHandle exh=new ExportHandle();
//			ArrayList list=(ArrayList)request.getAttribute("list");
//			if("1".equals(filetype))
//				exh.writeExcel(os, ExportHandle.CORP,"公司月运营情况", list);
//			if("2".equals(filetype))
//				exh.writeExcel(os, ExportHandle.INFO,"宏观信息", list);
			mySmartUpload.initialize(config, request, response);
			mySmartUpload.setContentDisposition(null);
//			mySmartUpload.downloadFile(filepath +cid+filename,null,Util.toUtf8String(filename));
			mySmartUpload.downloadFile(filepath +filename,null,new String(filename.getBytes("GBK"),"ISO-8859-1"));
		} catch (Exception e) {
//			Log.err(this.getClass(), "doPost", e);
			try {
				request.setAttribute("msg", "����ʧ��");
				request.getRequestDispatcher("../index/error.jsp").forward(request, response);
			} catch (Exception ex) {
			}
		}
	}

	/**
	 * Handles POST requests
	 */
	protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}
	/**
	 * Destroy the servlet
	 */
	public void destroy() {
		
	}
}
